﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml.Serialization;


namespace XMLSchemaTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        const string XSD_EXE_STI = @"C:\Users\RasmusBjørn\Desktop\XSD\Schemas\xsd.exe";
        const string SCHEMA_STI = @"C:\Users\RasmusBjørn\Desktop\XSD\Schemas\";
        const string CLASS_STI = @"C:\Users\RasmusBjørn\Desktop\XSD\Classes\";

        const string XML_STI = @"C:\Users\RasmusBjørn\Desktop\XSD\XML\";

        public MainWindow()
        {
            InitializeComponent();
            foreach (var item in Directory.GetFiles(SCHEMA_STI, "*.xsd"))
            {
                cboXSDFiler.Items.Add(Path.GetFileNameWithoutExtension(item));
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(XSD_EXE_STI);
            sb.Append(" " + SCHEMA_STI +cboXSDFiler.SelectedItem+".xsd");
            sb.Append(" /c /outputdir:"+CLASS_STI);

            ProcessStartInfo p = new ProcessStartInfo("cmd.exe", "/c" + sb.ToString());
            p.RedirectStandardInput = true;
            p.RedirectStandardOutput = true;
            p.UseShellExecute = false;
            p.WindowStyle = ProcessWindowStyle.Normal;
            Process.Start(p);
            
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Process.Start(CLASS_STI);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            foreach (var item in DeserializeXML<ProductFeed,ProductFeedProduct>())
            {
                MessageBox.Show(item.Key + " " + item.Value);

            }
            
           
        }
        /// <summary>
        /// Generisk metode til at håndterer XML filer fra xsd skemaer (Rasmus Speciale)
        /// </summary>
        /// <typeparam name="T">Gruppe_Feed</typeparam>
        /// <typeparam name="S">Gruppe_Feed_Gruppe</typeparam>
        /// <returns>Dictionary med Værdier og Nøgler</returns>
        public Dictionary<string, object> DeserializeXML<T, S>()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            XmlSerializer ser = new XmlSerializer(typeof(T));
            string fileName = "";

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = ".xml |*.xml";
            ofd.ShowDialog();
            fileName = ofd.FileName;

            T parsing = (T)ser.Deserialize(new FileStream(fileName, FileMode.Open));
           
            Type xmlType = parsing.GetType();
            Type xmlType2 = typeof(S);


            PropertyInfo[] info = xmlType.GetProperties();

            foreach (PropertyInfo item in info)
            {
                if (item.CanRead)
                {

                    object value = item.GetValue(parsing, new object[] { });
                    dic.Add(item.Name, value);
                }
            }



            return dic;
        }

        /* Den 'normale' / nemme måde :)
        XmlSerializer ser = new XmlSerializer(typeof(BrandFeed));

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = ".xml |*.xml";
            ofd.ShowDialog();
            string fileName = ofd.FileName;


            BrandFeed ba = (BrandFeed)ser.Deserialize(new FileStream(fileName, FileMode.Open));

            foreach (var item in ba.Brands)
            {
                MessageBox.Show(item.BrandCode + " " + item.BrandName);
            }
         * */
        

    }
}

